package com.ksl.kinglockguard.network.appupdate;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.ksl.kinglockguard.data.Constants;
import com.ksl.kinglockguard.utils.KingsSystemUtils;
import com.lsk.open.core.MyLog;

public class AppUpdateFileUtil {

	private static final String UPDATE_FILE_NAME_VERSION_CODE = "vcode.apk";
	
	public static String getUpdateApkFilePath(){
		return Constants.FOLDER_UPDATE + File.separator + Constants.APK_TYPE;
	}
	
	
	public static String getUpdateApkFilePathAndMakeDir(){
		return getUpdateApkDir() + File.separator + Constants.APK_TYPE;
	}
	
	
	public static String getUpdateApkDir(){
		File root = new File(Constants.FOLDER_UPDATE);
		if(!root.exists()){
			root.mkdirs();
		}
		return root.getAbsolutePath();
	}
	
	public static String getUpdateVersionDir(){
		String root = Constants.FOLDER_ROOT + File.separator + "vcode";
		return root + File.separator + UPDATE_FILE_NAME_VERSION_CODE;
	}
	
	public static String getUpdateVersionPathAndMakeDir(){
		File root = new File(Constants.FOLDER_ROOT + File.separator + "vcode");
		if(!root.exists()){
			root.mkdirs();
		}
		return root + File.separator + UPDATE_FILE_NAME_VERSION_CODE;
	}
	
	public static final String UPDATE_APK="blueFlash.apk";
	public static final String END_APK="end.apk";
	
	public static void refreshApkToRootUpdate(String aSrc){
		String targetPath = getRootUpdateAppPathAndMakeDir()+"/"+UPDATE_APK;
		MyLog.d("update","refreshAppFromUpdate --------- targetPath: " + targetPath);
		KingsSystemUtils.copyFileFromFile(aSrc, targetPath, true);
		File end = new File(getRootUpdateAppPathAndMakeDir()+"/"+END_APK);
		FileOutputStream fos = null;
		try {
		 fos = new FileOutputStream(end);
			fos.write("ok".getBytes());
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(fos!=null)
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static String getRootUpdateAppPath(){
		return Constants.PRIVATE_UPDATE;
	}
	
	public static String getRootUpdateAppPathAndMakeDir(){
		File root = new File(Constants.PRIVATE_UPDATE);
		if(!root.exists())
			root.mkdirs();
		
		return root.getAbsolutePath();
	}
	
}
